package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.autoconfig.huanxin.HuanXinTemplate;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.FriendService;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserLikeManager {

    @DubboReference
    private UserLikeService userLikeService;

    @DubboReference
    private RecommendUserService recommendUserService;

    @DubboReference
    private FriendService friendService;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private UserInfoService userInfoService;

    // 右滑喜欢
    public void cardLove(Long likeUserId) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc保存喜欢对方记录
        UserLike userLike = new UserLike();
        userLike.setCreated(System.currentTimeMillis()); // 喜欢时间
        userLike.setUserId(userId); // 你的id
        userLike.setLikeUserId(likeUserId); // 对方id
        userLikeService.save(userLike);
        // 3.判断双方是否为相互喜欢
        Boolean mutualLike = userLikeService.isMutualLike(userId, likeUserId);
        if (mutualLike) { // 如果是
            // 添加mongo好友
            friendService.addFriend(userId, likeUserId);
            // 添加环信好友
            huanXinTemplate.addContacts(userId, likeUserId);
        }
        // 4.删除此推荐用户
        recommendUserService.remove(userId, likeUserId);
    }

    // 左滑不喜欢
    public void cardUnlove(Long likeUserId) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.判断双方是否为相互喜欢
        Boolean mutualLike = userLikeService.isMutualLike(userId, likeUserId);
        // 如果是
        if (mutualLike) {
            // 删除mongo好友
            friendService.removeFriend(userId, likeUserId);
            // 删除环信好友
            huanXinTemplate.deleteContacts(userId, likeUserId);
        }
        // 3.删除此用户喜欢对方记录
        userLikeService.remove(userId, likeUserId);
        // 4.删除推荐用户信息
        recommendUserService.remove(userId, likeUserId);
    }

    // 我的统计
    public ResponseEntity myCounts() {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询统计数据
        UserLikeCountVo userLikeCountVo = userLikeService.findUserLikeCountVo(userId);
        // 3.返回vo
        return ResponseEntity.ok(userLikeCountVo);
    }

    // 我的详情
    public ResponseEntity myDetail(Integer type, Integer pageNum, Integer pageSize) {
        // 1.获取线程内userId
        Long userId = UserHolder.get().getId();
        // 2.调用rpc查询4种类型结果
        PageBeanVo pbv = userLikeService.findUserLikeVoByType(userId, type, pageNum, pageSize);
        // 3.完善userLikeVo
        List<UserLikeVo> voList = (List<UserLikeVo>) pbv.getItems();
        if (CollectionUtil.isNotEmpty(voList)) {
            for (UserLikeVo vo : voList) {
                // 查询对方用户详情
                UserInfo userInfo = userInfoService.findById(vo.getId());
                // 将信息设置到vo中
                BeanUtil.copyProperties(userInfo, vo );

                // 粉丝：判断是否相互喜欢
                vo.setAlreadyLove(userLikeService.isMutualLike(userId, vo.getId()));

            }
        }

        // 4.返回分页对象
        return ResponseEntity.ok(pbv);
    }
}
